<%--
  Created by IntelliJ IDEA.
  User: WY2015092804
  Date: 2015/10/19
  Time: 11:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="st" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="easyui" uri="http://www.joyi.cn/easyui/tags" %>
<%@ taglib prefix="joyi" uri="http://www.joyi.cn/tags" %>
<%@ taglib prefix="j" uri="j/tags" %>
<st:page title="员工管理">

    <div data-options="region:'west',title:'部门分类'," style="width: 300px">
        <table id="departmentList" class="easyui-treegrid"
               data-options="toolbar:'#addDepartment',url:'/user/req?rid=<j:des data="20151019000000000001"/>',idField:'id',treeField:'name',onDblClickRow:queryEmployeeByDepartmentId">
            <thead>
            <tr>
                <th data-options="field:'id',formatter:optDepartment,width:100">操作</th>
                <th data-options="field:'name',width:190">部门列表</th>
            </tr>
            </thead>
        </table>
        <div id="addDepartment">
            <easyui:linkButton name="添加部门" iconKey="add" onClick="editDepartment();"/>
        </div>
    </div>
    <div data-options="region:'center',title:'员工列表'">
        <joyi:dataGrid id="employeeList" key="getEmployeeList" toolbarId="addEmployee"/>
        <div id="addEmployee">
            <easyui:linkButton name="添加员工" iconKey="add" onClick="editEmployee();"/><br/>
                <easyui:hidden name="queryByDepartmentId" value=""/>
                姓名：<easyui:textbox name="employeeName" width="100"/>
                职位：<easyui:comboBox name="positionId" style="width:80px" params="method=query" selectedValue="isAll"  url="/user/employee/manager?act=position" dataOptions="multiple:true"/>
                性别：<easyui:comboBox name="gender" selectedValue="-1" params="method=query"   panelWidth="80" doName="employee/manager" actionName="gender"  style="width:80px"/>
                婚否：<easyui:comboBox name="marry" selectedValue="-1" params="method=query"  panelWidth="80" style="width:80px" doName="employee/manager" actionName="marry"/>
                出生日期：<easyui:date name="fromDate" editable="true" style="width:120px"/>-<easyui:date name="toDate" editable="true"  style="width:120px"/>
                <easyui:linkButton name="查询" iconKey="search" onClick="queryEmployee();"/>
        </div>
    </div>

    <script>
        //点击部门时显示部门下的员工
        function queryEmployeeByDepartmentId(index){
            $("#employeeList").datagrid("load",{
                departmentId:index.id
            });
            $("#queryByDepartmentId").attr("value", index.id);
        }

        function queryEmployee(){
            var dept = $("#queryByDepartmentId").attr("value");
            var posit = $("#positionId").combobox("getValues");
            var emp = $("#employeeName").textbox("getValue");
            var gender =$("#gender").combobox("getValue");
            var marry = $("#marry").combobox("getValue");
            var from = $("#fromDate").datebox("getValue");
            var to = $("#toDate").datebox("getValue");
            if(from && to && from>=to){
                alert("查询开始时间应在结束时间之前");
            } else{
                $("#employeeList").datagrid("load",{
                    departmentId:dept,
                    employeeName:emp,
                    positionId:posit,
                    gender:gender,
                    marry:marry,
                    fromDate:from,
                    toDate:to
                });
            }
        }
        function editEmployee(id) {
            var departmentId = $("#queryByDepartmentId").attr("value");
            var url = "/user/req?rid=<j:des data="20151020000000000009"/>";
            if(departmentId){
                url += "&departmentId="+departmentId;
            }
            if(id){
                url +="&employeeId="+id;
            }
            j.open(url,"editEmployee",{title:"编辑员工信息"},500,400);
        }

        function delEmployee(id){
            if(window.confirm("是否要删除？")){
                Http.post("/user/req?rid=<j:des data="20151020000000000011"/>&employeeId="+id,{},function(data){
                    alert(data.data);
                    reloadEmployeeDataGrid();
                });
            }
        }

        function optDepartment(value, data) {
            var opt = "<a href='javascript:void(0);' onclick=\"editDepartment('" + data.id + "')\">编辑</a>";
            opt += " <a href='javascript:void(0);' onclick=\"delDepartment('" + data.id + "')\">删除</a>";
            return opt;
        }

        function editDepartment(id) {
            if (id) {
                j.open("/user/req?rid=<j:des data="20151019000000000003"/>&id="+id, "editDepartment", {title: "编辑部门"}, 500, 400);
            } else {
                var row = $("#departmentList").treegrid("getSelected");
                if(row) {
                    j.open("/user/req?rid=<j:des data="20151019000000000003"/>&rowId=" + row.id, "addDepartment", {title: "添加部门"}, 500, 400);
                }
                else{
                    j.open("/user/req?rid=<j:des data="20151019000000000003"/>", "addDepartment", {title: "添加部门"}, 500, 400);
                }
            }
        }

        function delDepartment(id){
            if(window.confirm("是否要删除？")){
                Http.post("/user/req?rid=<j:des data="20151020000000000001"/>&departmentId="+id,{},function(data){
                    alert(data.data);
                    reloadDepartmentTree();
                });
            }
        }

        function reloadDepartmentTree(){
            $("#departmentList").treegrid("reload");
        }

        function reloadEmployeeDataGrid(){
            $("#employeeList").datagrid("load");
        }

    </script>
</st:page>
